package hit.core;


import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

public class SizeTest {
	Size size;
	
	@Before
	public void setUp() throws Exception {
		size = new Size();
	}

	@After
	public void tearDown() throws Exception {
		size = null;
	}

	@Test
	public void TestDefaultConstructor() {
		Size testSize = new Size();
		
		assertEquals(testSize.GetAmount(), 0.0, .00001);
		assertEquals(testSize.GetType(), Size.Unit.COUNT);
	}
	
	@Test
	public void TestConstructor() {
		Size size1 = new Size(10, Size.Unit.COUNT);
		assertEquals(size1.GetAmount(), 10, .0001);
		assertEquals(size1.GetType(), Size.Unit.COUNT);
		
		Size size2 = new Size((float) 5.5, Size.Unit.KILOGRAMS);
		assertEquals(size2.GetAmount(), 5.5, .0001);
		assertEquals(size2.GetType(), Size.Unit.KILOGRAMS);
	}
	
	@Test
	public void TestIsValid() {
		assertTrue(Size.IsValid(new Size(1, Size.Unit.COUNT)));
		assertTrue(Size.IsValid(new Size(2.2, Size.Unit.GRAMS)));
		assertFalse(Size.IsValid(new Size(4.5, Size.Unit.COUNT)));
	}
	
	@Test
	public void TestGetAndSetAmount() {
		assertEquals(size.GetAmount(), 0.0, .0001);
		
		size.SetAmount(3);
		assertEquals(size.GetAmount(), 3, .0001);
		
		size.SetAmount(1);
		assertEquals(size.GetAmount(), 1, .0001);
	}
	
	@Test(expected=IllegalArgumentException.class) 
	public void TestInvalidSetAmount() {
		size.SetAmount(3.3);
	}
	
	@Test
	public void TestGetAndSetType() {
		assertEquals(size.GetType(), Size.Unit.COUNT);
		
		size.SetType(Size.Unit.GALLONS);
		assertEquals(size.GetType(), Size.Unit.GALLONS);
		
		size.SetType(Size.Unit.QUARTS);
		assertEquals(size.GetType(), Size.Unit.QUARTS);
	}
	
	/**
	 * Unfortunately, this relies upon the SetType and SetAmount methods
	 * */
	@Test(expected=IllegalArgumentException.class)
	public void TestInvalidSetType() {
		size.SetType(Size.Unit.GALLONS);
		size.SetAmount(3.3);
		size.SetType(Size.Unit.COUNT);
	}
	
	@Test
	public void TestSizeConversions() {
		size.SetType(Size.Unit.GRAMS);
		size.SetAmount(520.25);
		
		assertNull(size.ConvertTo(Size.Unit.LITERS));
		assertNull(size.ConvertTo(Size.Unit.COUNT));
		
		Size test = size.ConvertTo(Size.Unit.KILOGRAMS);
		assertEquals(test.GetType(), Size.Unit.KILOGRAMS);
		assertEquals(test.GetAmount(), 0.52025, 0.0001);
		
		test = size.ConvertTo(Size.Unit.POUNDS);
		assertEquals(test.GetType(), Size.Unit.POUNDS);
		assertEquals(test.GetAmount(), 1.147, 0.0001);
		
		test = size.ConvertTo(Size.Unit.OUNCES);
		assertEquals(test.GetType(), Size.Unit.OUNCES);
		assertEquals(test.GetAmount(), 18.351, 0.0001);
		
		size.SetType(Size.Unit.PINTS);
		size.SetAmount(42);
		
		assertNull(size.ConvertTo(Size.Unit.KILOGRAMS));
		assertNull(size.ConvertTo(Size.Unit.COUNT));
		
		test = size.ConvertTo(Size.Unit.GALLONS);
		assertEquals(test.GetType(), Size.Unit.GALLONS);
		assertEquals(test.GetAmount(), 5.25, 0.01);
		
		test = size.ConvertTo(Size.Unit.FLUID_OUNCES);
		assertEquals(test.GetType(), Size.Unit.FLUID_OUNCES);
		assertEquals(test.GetAmount(), 672, 0.2);
		
		test = size.ConvertTo(Size.Unit.QUARTS);
		assertEquals(test.GetType(), Size.Unit.QUARTS);
		assertEquals(test.GetAmount(), 21, 0.1);
		
		test = size.ConvertTo(Size.Unit.LITERS);
		assertEquals(test.GetType(), Size.Unit.LITERS);
		assertEquals(test.GetAmount(), 19.87, 0.1);
	}
}


